/*
-----------------------------------------------------------------------------
This source file is part of as4game
	(ActionScript For Game Engine)
For the latest info, see http://as4game.googlecode.com

Copyright (c) 2000-2011 Hproof Software Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
package org.as4game.common.data
{

	/**
	 * 双向链表
	 * <li> 链表可以保存任意类型的数据, 但该数据必须包含  __prev, __next 属性
	 * <li> 1个数据, 同时只能保存在1个链表中
	 */
	public class List2
	{
		private var _root:Object; 
		private var _count:int;
		
		/**
		 */
		public function List2(){
			_root = {};
			clear( false );
		}
		
		/**
		 * 清空列表
		 * @param clearLinkInfo 是否清空对象中的  __prev/__next 属性
		 */
		public function clear( clearLinkInfo:Boolean ):void{
			if( clearLinkInfo ){
				forEach( function(obj:Object):void{
					obj.__prev = obj.__next = null;
				});
			}
			_root.__next = _root.__prev = _root;
			_count = 0;
		}
		
		/**
		 * 获取数量
		 */
		public function get count():int{
			return _count;
		}
		
		/**
		 * 添加元素 
		 * @param obj 被添加的元素
		 * @return 返回 obj
		 */
		public function pushHead( obj:Object ):Object{
			if( obj.__next || obj.__prev ){
				throw new Error("1个对象, 同时只能保存在1个链表中");
			}
			
			var root:Object = _root;
			obj.__next = root.__next;
			obj.__prev = root;
			obj.__next.__prev = obj;
			obj.__prev.__next = obj;
			++ _count;
			return obj;
		}
		public function pushTail( obj:Object ):Object{
			if( obj.__next || obj.__prev ){
				throw new Error("1个对象, 同时只能保存在1个链表中");
			}
			
			var root:Object = _root;
			obj.__next = root;
			obj.__prev = root.__prev;
			obj.__next.__prev = obj;
			obj.__prev.__next = obj;
			++ _count;
			return obj;
		}
		
		/**
		 * 删除元素
		 */
		public function popHead():Object{
			var root:Object = _root;
			var obj:Object = root.__next;
			if( obj == root ) return null;
			
			obj.__prev.__next = obj.__next;
			obj.__next.__prev = obj.__prev;
			obj.__prev = obj.__next = null;
			-- _count;
			return obj;
		}
		public function popTail():Object{
			var root:Object = _root;
			var obj:Object = root.__prev;
			if( obj == root ) return null;
			
			obj.__prev.__next = obj.__next;
			obj.__next.__prev = obj.__prev;
			obj.__prev = obj.__next = null;
			-- _count;
			return obj;
		}
		
		/**
		 * 获取, 不删除
		 */
		public function peekHead():Object{
			var root:Object = _root;
			var obj:Object = root.__next;
			return (obj == root? null: obj);
		}
		public function peekTail():Object{
			var root:Object = _root;
			var obj:Object = root.__prev;
			return (obj == root? null: obj);
		}
		
		/**
		 * 删除指定元素
		 */
		public function removeAt( obj:Object ):Object{
			obj.__prev.__next = obj.__next;
			obj.__next.__prev = obj.__prev;
			
			obj.__prev = obj.__next = null;
			-- _count;
			return obj;
		}
		
		/**
		 * 移动位置
		 */
		public function moveToHead( obj:Object ):void{
			
			obj.__prev.__next = obj.__next;
			obj.__next.__prev = obj.__prev;
			
			var root:Object = _root;
			obj.__next = root.__next;
			obj.__prev = root;
			obj.__next.__prev = obj;
			obj.__prev.__next = obj;
		}
		
		/**
		 * 遍历每个对象, 执行函数 fn(obj)
		 * @param fn 回调函数, 原型为 function fn( obj:Object );
		 */
		public function forEach( fn:Function ):void{
			var root:Object = _root;
			var node:Object = root.__next;
			while( node != root){
				fn( node );
				node = node.__next;
			}
		}
		
		/**
		 * 字符串形式
		 */
		public function toString():String{
			var arr:Array = [ "[List2] count=" + _count ];
			forEach( function(obj:Object):void{
				arr.push( obj );
			});
			var str:String = arr.join( "\r" );
			return str;
		}
	}
}